package com.lingyuan.discounts.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingyuan.common.core.domain.R;
import com.lingyuan.common.core.domain.vo.oms.OrderVo;
import com.lingyuan.common.core.domain.vo.sms.CouponMemberVo;
import com.lingyuan.common.core.domain.vo.sms.UpdateCouponHistoryParam;
import com.lingyuan.common.core.exception.ServiceException;
import com.lingyuan.common.security.utils.SecurityUtils;
import com.lingyuan.discounts.constant.OrderConstant;
import com.lingyuan.discounts.domain.Coupon;
import com.lingyuan.discounts.domain.request.CouponHistoryRequest;
import com.lingyuan.discounts.domain.CouponHistory;
import com.lingyuan.discounts.domain.CouponHistoryResponse;
import com.lingyuan.discounts.enumParam.GetType;
import com.lingyuan.discounts.enumParam.UseType;
import com.lingyuan.discounts.mapper.CouponHistoryMapper;
import com.lingyuan.discounts.mapper.CouponMapper;
import com.lingyuan.discounts.remote.OrderRemoteService;
import com.lingyuan.discounts.service.CouponHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: Li Hao Xuan
 * @name: CouponHistoryServiceImpl
 * @Description TODO
 */
@Service
public class CouponHistoryServiceImpl extends ServiceImpl<CouponHistoryMapper,CouponHistory> implements CouponHistoryService {

    /**
     * 查询优惠券领取记录
     * @param couponHistoryRequest
     * @return
     */
    @Autowired
    private CouponHistoryMapper couponHistoryMapper;

    @Autowired
    private OrderRemoteService orderRemoteService;

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<CouponHistoryResponse> findHistoryList(CouponHistoryRequest couponHistoryRequest) {

        List<CouponHistoryResponse> list = couponHistoryMapper.findHistoryList(couponHistoryRequest);

        return list;
    }

    @Override
    public void addHistory(CouponHistory couponHistory) {
        /**获取当前登录用户信息*/
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        /**设置会员信息*/
        couponHistory.setMemberId(userId);
        couponHistory.setMemberNickName(username);
        /**
         * 默认获取方式[1,"手动领取"]
         */
        couponHistory.setGetType(GetType.MANUAL_PICKUP.getCode());
        /**
         * 默认使用状态[0,"未使用"]
         */
        couponHistory.setUseType(UseType.UNUSED.getCode());
        /**
         * 创建时间
         */
        LocalDateTime now = LocalDateTime.now();
        couponHistory.setCreateTime(now);
        /**
         * 使用时间
         */
        couponHistory.setUseTime(null);

        couponHistoryMapper.addHistory(couponHistory);
    }

    @Override
    public void updHistory(CouponHistory couponHistory) {
        this.updateById(couponHistory);
    }

    @Override
    public void delHistory(Long id) {
        this.removeById(id);
    }

    // 更新优惠券使用状态/使用时间/订单号
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updHistoryStatus(UpdateCouponHistoryParam param) {
        CouponHistory couponHistory = new CouponHistory();
        // 设置使用状态
        couponHistory.setUseType(UseType.HAVE_BEEN_USED.getCode());
        // 设置使用时间
        couponHistory.setUseTime(LocalDateTime.now());
        /**
         * 订单信息
         */
        String orderStr = stringRedisTemplate.opsForValue().get(OrderConstant.SUBMIT_ORDER_KEY + param.getOrderSn());
        OrderVo orderEntity = JSON.parseObject(orderStr, OrderVo.class);
        couponHistory.setOrderSn(orderEntity.getOrderSn());
        couponHistory.setCouponId(param.getCouponId());
        // 更新优惠券使用数量
        couponMapper.updUseCount(param.getCouponId());
        // 使用优惠券
        couponHistoryMapper.updHistoryStatus(couponHistory);
    }

    @Override
    public List<Coupon> findCouponId(Long id) {
        return couponHistoryMapper.findCouponId(id);
    }


}
